package cn.pxy.ysframework.web.Controller;

import cn.pxy.ysframework.utils.Message;
import cn.pxy.ysframework.utils.StatusMessage;
import cn.pxy.ysframework.web.Entity.Account;
import cn.pxy.ysframework.web.Service.IAccountService;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping(value = "/Account")
@Api(description = "账户控制器--已完成")
public class AccountController {

    @Autowired
    private IAccountService accountService;

    /**
     * 登录
     * @param account
     * @return
     */
    @PostMapping(value = "/Login")
    public Message Login(Account account){
        Account acc = accountService.getAccountByAccountnameAndPassword(account.getAccountname(), account.getPassword());
        if(acc==null){
            return new Message("601",StatusMessage.ACCOUNTANDPASSWORD_ERROR);
        }else{
            return new Message(JSON.toJSONString(acc));
        }
    }

    /**
     * 注册
     * @param account
     * @return
     */
    @PostMapping(value = "/Register")
    public Message Register(@RequestBody Account account){
        if (StringUtils.isBlank(account.getAccountname())&&StringUtils.isBlank(account.getPassword()))
            return new Message("601",StatusMessage.NO_EMPTY);
        Account acc = accountService.getAccountByUserName(account.getAccountname());
        if(acc!=null){
            return new Message("601",StatusMessage.USER_EXIST);
        }else{
            Account register = accountService.Register(account);
            return register==null? new Message("601",StatusMessage.FAIL):new Message();
        }
    }
}
